
package com.dps.omc.action;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dps.omc.model.User;
import com.dps.omc.model.Handler;
import com.dps.omc.service.IHandlerService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dps.common.domain.view.BizData4Page;
import com.dps.common.utils.IdGenerator;
import com.dps.common.utils.JsonResult;
import com.dps.common.utils.PageParam;
import com.dps.common.utils.Constants;

@Controller
public class HandlerAction extends AbstractAdminController<IHandlerService>{

    @Autowired
    private IHandlerService handlerService;
    
    @RequestMapping(value="/handler/list")
    public String list(Handler handler, ModelMap model, PageParam pageParam, HttpServletRequest request, HttpServletResponse response){

        //获取参数
    	Map<String, Object> conditions = getQueryMap(handler); 	
    	BizData4Page<Handler> pageCtx = doPage(model, conditions, pageParam);
    	model.addAttribute("handler", handler);
    	return "/module/handler/list";
    }    
    
    @RequestMapping(value="/handler/add")
    public String add(ModelMap model,HttpServletRequest request,HttpServletResponse response){

    	model.addAttribute("handler", new Handler());
        return "module/handler/edit";
    }    
    
    @RequestMapping(value="/handler/edit")
    public String edit(String id,ModelMap model,HttpServletRequest request,HttpServletResponse response){
    	
    	Handler handler = handlerService.fetch(id);
    	model.addAttribute("handler", handler);
        return "module/handler/edit";
    }    
    
    @RequestMapping(value="/handler/save")
    @ResponseBody
    public String save(HttpServletRequest request,Handler handler){
		boolean isAddNew = StringUtils.isBlank(handler.getId())?true:false;
        User user=getCurrentUser();
		if(isAddNew)
		{
			handler.setId(IdGenerator.createNewId());
            handler.setCreaterId(user.getId());
            handler.setCreaterName(user.getRealName());
			handlerService.add(handler);
		}
		else
		{
			handlerService.update(handler);
		}
        return JsonResult.saveSuccessClose(Constants.MAIN_TAB).toString();
    }    
    
    @RequestMapping(value="/handler/delete")
    @ResponseBody
    public String delete(String ids,HttpServletRequest request){
    	
    	handlerService.deleteByIds(ids);
    	return JsonResult.DELETE_SUCCESS.toString();
    }       
	
    protected Map getQueryMap(Handler handler)
    {
    	Map<String, Object> conditions = new HashMap();
    	
		conditions.put("clazz", handler.getClazz());		
		conditions.put("note", handler.getNote());		
		conditions.put("createrId", handler.getCreaterId());		
		conditions.put("createrName", handler.getCreaterName());		
		conditions.put("createDate", handler.getCreateDate());		
    	return conditions;
    }

    @Override
    protected IHandlerService getMainService() {
        return handlerService;
    }

    @Override
    protected String getMainObjName() {
        return "handler";
    }

    @Override
    protected String getViewTitle() {
        return "handler";
    }
}
